/*
 * @Author: your name
 * @Date: 2021-09-06 10:50:38
 * @LastEditTime: 2021-09-08 17:03:38
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \common-vue\designMode\观察者模式\index.js
 */
function Dep() {
  this.deps = []
}
Dep.prototype.addDep = function(dep) {
  this.deps.push(dep)
}

Dep.prototype.notify = function() {
  if(this.deps.length === 0) {
    console.log('更新列表为空')
    return
  }
  this.deps.forEach(dep => dep.update())
}

function BuyCar(type) {
  this.type = type
}

BuyCar.prototype.addCart = addCar
BuyCar.prototype.updateCatNum = updateCatNum
BuyCar.prototype.update = function() {
  const type = this.type
  console.log('type', type)
  if(type === 1) {
    BuyCar.prototype.addCart()
  } else {
    BuyCar.prototype.updateCatNum()
  }
}
function addCar(){
  console.log('添加购物车')
}
function updateCatNum(){
  console.log('更新购物车数量')
}

const dep = new Dep()
const b1 = new BuyCar(1)
dep.addDep(b1)
dep.notify()